package com.axjy.service.impl;

import com.axjy.mapper.MenuMapper;
import com.axjy.mapper.PermissionMapper;
import com.axjy.mapper.UserMapper;
import com.axjy.pojo.Menu;
import com.axjy.pojo.Permission;
import com.axjy.pojo.User;
import com.axjy.service.PermissionService;
import com.axjy.vo.NodeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service("permissionService")
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public List<NodeVo> search4Tree(Integer roleId) {

        List<Integer> menuIdList=permissionMapper.findMenuId(roleId);

        List<NodeVo> list01 = new ArrayList<>();
        List<Menu> menuList01 = menuMapper.findByPid(null);
        if (menuList01 != null && menuList01.size() > 0) {
            for (Menu menu01 : menuList01) {
                NodeVo nodeVo01 = new NodeVo();
                nodeVo01.setId(menu01.getId());
                nodeVo01.setText(menu01.getName());
                if(menuIdList.contains(menu01.getId())) {
                    nodeVo01.getState().setChecked(true);
                }
                List<NodeVo> list02 = new ArrayList<>();
                nodeVo01.setNodes(list02);
                list01.add(nodeVo01);

                //为每一个一级菜单设置二级菜单
                List<Menu> menuList02 = menuMapper.findByPid(menu01.getId());
                if (menuList02 != null && menuList02.size() > 0) {
                    for (Menu menu02 : menuList02) {
                        NodeVo nodeVo02 = new NodeVo();
                        nodeVo02.setId(menu02.getId());
                        nodeVo02.setText(menu02.getName());
                        if(menuIdList.contains(menu02.getId())) {
                            nodeVo02.getState().setChecked(true);
                        }
                        list02.add(nodeVo02);
                    }
                }
            }
        }
        return list01;
    }

    @Override
    public void grant(Integer roleId, Integer[] menuIds) {
        permissionMapper.del(roleId);
        if(menuIds!=null&&menuIds.length>0){
            for(Integer menuId:menuIds){
                permissionMapper.add(roleId,menuId);
            }
        }
    }

    @Override
    public User login(String name, String pwd) {
        return userMapper.findByNameAndPwd(name,pwd);
    }

    @Override
    @Transactional(readOnly = true)
    public Map<Menu, List<Menu>> createMenuBar(User loginUser) {
        Map<Menu, List<Menu>> menuBar=new TreeMap<>();
        Integer roleId=loginUser.getRoleId();
        List<Integer> menuIds=permissionMapper.findMenuId(roleId);
        if(menuIds!=null&&menuIds.size()>0){
            for(Integer menuId:menuIds){
                Menu level01=menuMapper.findById(menuId);
                if(level01.getPId()==null){
                    List<Menu> level02=new ArrayList<>();
                    menuBar.put(level01,level02);
                    for(Integer m:menuIds){
                        Menu menu=menuMapper.findById(m);
                        if(menu.getPId()!=null&&menu.getPId()==level01.getId()){
                            level02.add(menu);
                        }
                    }
                }
            }
        }
        return menuBar;
    }
}
